package com.zzx.project.controller;

import com.zzx.project.entity.*;
import com.zzx.project.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

//页面跳转Controller
@Controller
@RequestMapping("page")
public class PageController {

    @Autowired
    HttpSession session;
    @Autowired
    MaximService maximService;
    @Autowired
    PracticalService practicalService;
    @Autowired
    TagService tagService;
    @Autowired
    UserService userService;
    @Autowired
    BackService backService;
    @Autowired
    LmService lmService;
    @Autowired
    ArticleService articleService;
    @Autowired
    WlmService wlmService;

    //未登录时的跳转提示（供任何页面调用）
    @RequestMapping("noLogin")
    public String noLogin(HttpServletRequest request) {
        request.getSession().setAttribute("tip", request.getParameter("tip"));
        return "redirect:/page/toLogin";
    }

    //跳转实用网站页面
    @GetMapping("practicalWeb")
    public String practicalWeb(Model model) {
        if (checkInit(session) == null) {
            return "redirect:/";
        }
        model.addAttribute("webs", practicalService.getAll());
        return "main/practicalWeb";
    }

    //跳转读者墙页面
    @GetMapping("readerWall")
    public String toReaderWall(Model model) {
        if (checkInit(session) == null) {
            return "redirect:/";
        }
        model.addAttribute("users", userService.getAll());
        model.addAttribute("data", wlmService.getAllData());
        session.setAttribute("fiveData", wlmService.getFiveData());
        return "main/readerWall";
    }

    //跳转标签墙页面
    @GetMapping("tagWall")
    public String toTagWall(Model model) {
        if (checkInit(session) == null) {
            return "redirect:/";
        }
        List<Tag> tags = tagService.getAll();
        model.addAttribute("tags", tags);
        return "main/tagWall";
    }

    //跳转后台
    @GetMapping("backstage")
    public String toBackstage(HttpServletRequest request, Model model) {
        if (checkInit(session) == null) {
            return "redirect:/";
        }
        User user = (User) request.getSession().getAttribute("user");
        //当用户是3级则可以进入管理员页面
        if (user == null || user.getUserlevel() != 3) {
            return "redirect:/";
        } else {
            //更新数据库后台表统计内容
            backService.updateInfo(user.getUserid());
            model.addAttribute("articleCount", articleService.getAllArticleCount());
            model.addAttribute("practicalCount", practicalService.getAllCount());
            model.addAttribute("lmCount", lmService.getMessageCount());
            model.addAttribute("backInfo", backService.getBackInfo());
            model.addAttribute("opNum", userService.getOpNum());
            return "backstage/main";
        }
    }

    //跳转密码修改页面
    @GetMapping("fixpass")
    public String toFixpass() {
        return "fixPass";
    }

    //跳转写文章页面
    @GetMapping("write")
    public String toWrite(Model model) {
        if (checkInit(session) == null) {
            return "redirect:/";
        }
        List<Tag> tags = tagService.getAll();
        model.addAttribute("tags", tags);
        return "write/index";
    }

    //跳转文章内容页面
    @GetMapping("article")
    public String toArticle() {
        if (checkInit(session) == null) {
            return "redirect:/";
        }
        return "main/content";
    }

    //跳转个人信息页
    @GetMapping("about")
    public String toAbout(HttpServletRequest request, Model model) {
        //visitor参数不为空时进行的访客模式操作，为空则直接跳转
        if (request.getParameter("visitor") != null) {
            int visitor = Integer.parseInt(request.getParameter("visitor"));
            int uid = 0;
            String uname = null;
            if (request.getParameter("uid") != null) {
                uid = Integer.parseInt(request.getParameter("uid"));
                User user = userService.getUserById(uid);
                List<Leavingmessage> messages = lmService.getMessagesById(uid);
                model.addAttribute("messages", messages);
                model.addAttribute("visitor", visitor);
                model.addAttribute("user", user);
                model.addAttribute("uid", uid);
            } else {
                uname = request.getParameter("uname");
                User user = userService.getUserByName(uname);
                List<Leavingmessage> messages = lmService.getMessagesById(user.getUserid());
                model.addAttribute("messages", messages);
                model.addAttribute("visitor", visitor);
                model.addAttribute("user", user);
                model.addAttribute("uid", user.getUserid());
            }
        }
        return "main/about";
    }

    //跳转个人信息页
    @GetMapping("toLogin")
    public String toLogin(HttpServletRequest request, Model model) {
        //visitor参数不为空时进行的访客模式操作，为空则直接跳转
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("userInfo")) {
                model.addAttribute("findCookie", 1);
                break;
            }
        }
        return "index";
    }

    //跳转相关文章页
    @GetMapping("relevant/{column}")
    public String toRelevant(@PathVariable("column") int column, HttpServletRequest request, Model model) {
        if (checkInit(session) == null) {
            return "redirect:/";
        }
        List<Article> randomArticle = (List<Article>) request.getSession().getAttribute("randomArticle");
        if (randomArticle == null) {
            randomArticle = articleService.getRandomArticle();
            List<Article> randomArticle1 = new ArrayList<>();
            for (Article article : randomArticle) {
                String[] audiences = article.getArticleaudience().split(",");
                article.setArticleaudiences(audiences.length - 1);
                randomArticle1.add(article);
            }
            request.getSession().setAttribute("randomArticle", randomArticle1);
        }
        List<Article> relevantArticle = articleService.getArticleByColumn(column);
        List<Article> list = new ArrayList<>();
        for (Article article : relevantArticle) {
            int articleAudiences = article.getArticleaudience().split(",").length - 1;
            article.setArticleaudiences(articleAudiences);
            String[] tags = article.getArticletags().split(",");
            List<String> tagList = new ArrayList<>();
            for (String tag : tags) {
                tagList.add(tag);
            }
            article.setArticletaglist(tagList);
            list.add(article);
        }
        model.addAttribute("relevantArticles", list);
        String columnName = null;
        switch (column) {
            case 1:
                columnName = "前端技术";
                break;
            case 2:
                columnName = "后端技术";
                break;
            case 3:
                columnName = "资源分享";
                break;
            case 4:
                columnName = "其他内容";
                break;
            default:
                columnName = "XXXX";
                break;
        }
        model.addAttribute("columnName", columnName);
        return "main/relevant";
    }

    public String checkInit(HttpSession session) {
        if (session.getAttribute("randomArticle") == null || session.getAttribute("fiveData") == null) {
            return null;
        }
        return "has";
    }
}